#-*- coding: UTF-8 -*-
import math
# def add(x,y,f):
#     return f(x)+f(y)
# print add(225,256,math.sqrt)
#
# def f(x):
#     return x*x
# print map(f,[1,2,3,4,5,6,7,8,9,11,12,45,456,693,542,54])
import time

'''
def format_name(s):
    s.replace(' ', '')
    s.strip()
    return s[0].upper() + s[1:].lower()
print map(format_name,[' aj kj','Fm ajg','fne rj',' df gre',' L J KL','J KJ S',' DD SFS',' S DJFS',' S D FSD'])


def funtion1(x):
    return x*x
fm = map(funtion1,[22,34,54,454,64,73,353,53,54])
print sorted(fm)

def is_not_empty(s):
    return s and s.strip() and len(s.strip())>0
print filter(is_not_empty,['',' test','',' None','str','','END'])
'''
# 阶乘
# def factorial(f): # 定义高阶函数
#     def fn(*args,**kw):
#         star = time.time()
#         r =f(*args,**kw)
#         end = time.time()
#         print 'call factorial in',str(end-star)
#         return r
#     return fn  #返回函数
# @factorial
# def factorial(n):
#     return reduce(lambda x,y: x*y,range(1,n+1))
# print factorial(900)

# import time
# from functools import reduce
# def performace(f):
#    def fn(*args,**kw):
#        star= time.time()
#        f1=f(*args,**kw)
#        end=time.time()
#        print 'call factorial() in', str(end - star)
#        return f1
#    return fn
# @performace
# def factorial(n):
#     return reduce(lambda x,y:x*y,range(1,n+1))  #阶乘
# print factorial(100000)

'''
#希望打印出'[INFO] call xxx()...'，有的函数不太重要，希望打印出'[DEBUG] call xxx()...'，
# 这时，log函数本身就需要传入'INFO'或'DEBUG'这样的参数，
@log('DEBUG')
def my_func():
    pass
@log('INFO')
def my_func():
    pass
# 把上面的定义翻译成高阶函数的调用

my_func= log('DEBUG')(my_func)
log_decorator = log（'DEBUG')
my_func=log_decorator(my_func)

log_decorator  = math.log('DEBUG')
@log_decorator
def my_func():
    pass


'''
import logging

logging.debug("This is a debug log.")
logging.info("This is a info log.")
logging.warning("This is a warning log.")
logging.error("This is a error log.")
logging.critical("This is a critical log.")
logging.log(logging.DEBUG, "This is a debug log.")
logging.log(logging.INFO, "This is a info log.")
logging.log(logging.WARNING, "This is a warning log.")
logging.log(logging.ERROR, "This is a error log.")
logging.log(logging.CRITICAL, "This is a critical log.")


打印运行时间
def performance(unit):
    def new_fn(f):
        def fn(*args,**kw):
            star =time.time()
            m = f(*args,**kw)
            end = time.time()
            if unit =='ms':
                print 'call factorial() in' ,str(end-star)*1000
            else:
                print 'call factorial() in', str(end - star)
            return m
            return fn
    return new_fn
@performance
def factorial(ms):
    return reduce(lambda x,y:x*y,range(1,10010))
print factorial(10010)



def log(prefix):
    def log_decorator(f):
        def wrapper(*args,**kw):
            print '[%s] %s()...' % (prefix,f._name_)
            return f(*args,**kw)
            return wrapper
    return log_decorator
@log('DEBUG')
def test():
    sum = reduce(lambda x,y:x*y,range(1,1000))
pass
print test()
'''


import time
import  functools

def performance(unit):
    def perf_decorator(f):
        @functools.wraps(f)
        def f1(*args,**kw):
            star = time.time()
            r = f(*args, **kw)
            end = time.time()
            if unit == 'ms':
                print 'call factorial() in', str(end - star) * 1000
            else:
                print 'call factorial() in', str(end - star)
            return
        return f1
    return perf_decorator

@performance('ms')
def factorial(n):
    return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10010)
print factorial.__name__















#
# def f1(x):
#     pass
# print f1.__name__
# def log(f):
#     def wrapper(*args,**kw):
#         print 'call...'
#         return f(*args,**kw)
#     return wrapper
# @log
# def f2(x):
#     pass
# print f2.__name__